package com.bestcem.xm.qdes.handler.impor;

import com.alibaba.fastjson.JSONArray;
import com.alibaba.fastjson.JSONObject;

import java.util.Arrays;
import java.util.List;
import java.util.regex.Pattern;
import java.util.stream.Collectors;

public interface KeyWordHandler {
    Pattern T_PATTERN = Pattern.compile("^(0|不限|整数|小数|字母|中文|邮件|日期|手机号码|固话|手机/固话|身份证号)$");
    Pattern Y_PATTERN = Pattern.compile("^(年-月-日|年-月|年)$");
    Pattern RANGE_PATTERN = Pattern.compile("^(\\d+-\\d+-\\d+|\\d+-\\d+)$");

    Integer handler(String qType, JSONArray rawItems, JSONObject questionDict);

    String getType();

    default JSONObject parseOptionOpen(List<String> attrs) {
        // 开放框解析
        JSONObject openAttrs = new JSONObject();
        openAttrs.put("range", new JSONArray());
        if (attrs == null || attrs.isEmpty()) {
            // 没有开放设置，使用默认设置: 必填，不限
            openAttrs.put("required", true);
            openAttrs.put("type", 0);
            openAttrs.put("unique", "none");
        } else {
            for (String oattr : attrs) {
                if ("必填".equals(oattr)) {
                    openAttrs.put("required", true);
                } else if ("唯一".equals(oattr)) {
                    openAttrs.put("unique", true);
                } else {
                    if (RANGE_PATTERN.matcher(oattr).find()) {
                        openAttrs.put("range", Arrays.stream(oattr.split("-")).map(Integer::parseInt).collect(Collectors.toList()));
                    } else if (T_PATTERN.matcher(oattr).find()) {
                        openAttrs.put("type", deOpenContentLimitTrans(oattr));
                    } else if (Y_PATTERN.matcher(oattr).find()) {
                        String rangeTxt = "";
                        switch (oattr) {
                            case "年":
                                rangeTxt = "yyyy";
                                break;
                            case "年-月":
                                rangeTxt = "yyyy-MM";
                                break;
                            case "年-月-日":
                                rangeTxt = "yyyy-MM-dd";
                                break;
                            default:
                                rangeTxt = "";
                        }
                        openAttrs.put("range", rangeTxt);
                    }
                }
            }
        }
        if (!openAttrs.isEmpty()) {
            JSONObject re = new JSONObject();
            re.put("open_attrs", openAttrs);
            return re;
        }
        return new JSONObject();
    }

    default Object deOpenContentLimitTrans(String openType) {
        // 开放框内容限制 文本反推
        if ("不限".equals(openType)) {
            return 0;
        } else if ("整数".equals(openType)) {
            return "int";
        } else if ("小数".equals(openType)) {
            return "number";
        } else if ("字母".equals(openType)) {
            return "lang_en";
        } else if ("中文".equals(openType)) {
            return "lang_ch";
        } else if ("邮件".equals(openType)) {
            return "email";
        } else if ("日期".equals(openType)) {
            return "date";
        } else if ("手机号码".equals(openType)) {
            return "mobile";
        } else if ("固话".equals(openType)) {
            return "tel";
        } else if ("手机/固话".equals(openType)) {
            return "mobile_tel";
        } else if ("身份证号".equals(openType)) {
            return "idCard";
        }
        return null;
    }
}
